﻿/// Name - Admin/EditHelpText.cs
/// Description - Allows the administrator to update the help text that will be shown on questionaire
///     questions.
/// Programmer - Dan Yatzeck
/// Date coded - 11/25/2011
/// Arguments - None
/// Variables -
///     _presenter - Object linking to parent Presenter
/// Files accessed by this code artifact - None
/// Files changed by this code artifact - None
/// I/O -
///     Input - Question list to be edited
///     Output - Web page, updated help text
/// Error Handling - None
/// Modifications -
///     1.0 - Initial coding
/// Known Faults -
///     1.0 - HTML encoding may be confusing to users

using System;
using Microsoft.Practices.ObjectBuilder;
using Common;

namespace CSC440TeamOne.Admin.Views
{
    /// <summary>
    /// Give the options to update help text for questionaire questions
    /// </summary>
	public partial class EditHelpText : Microsoft.Practices.CompositeWeb.Web.UI.Page, IEditHelpTextView
	{
		private EditHelpTextPresenter _presenter;

        /// <summary>
        /// Page load
        /// Binds question data to listbox, resets selected value on postback
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
            
			if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();

            lbxQuestions.DataSource = this._presenter.getQuestionTitles();
            lbxQuestions.DataBind();

            if (this.IsPostBack)
            {
                lbxQuestions.SelectedValue = Request.Form[lbxQuestions.UniqueID];
            }

            lbxQuestions.SelectedIndexChanged += new EventHandler(lbxQuestions_SelectedIndexChanged);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            
		}

        /// <summary>
        /// When this object is instantiated, create the presenter link
        /// </summary>
        [CreateNew]
		public EditHelpTextPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

        /// <summary>
        /// Listbox selected index changed event handler
        /// Gets the question id from the selected value, then gets the corresponding
        /// help text object from the question list.  Displays help text values in
        /// associated text boxes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void lbxQuestions_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get question ID
            string questionID = lbxQuestions.SelectedValue.Split(' ')[0];

            int parsedQuestionID;
            if (int.TryParse(questionID, out parsedQuestionID))
            {
                // Get HelpText from question and display
                HelpText helpText = this._presenter.getHelpText(parsedQuestionID);

                if (helpText != null)
                {
                    txtHeader.Text = Server.HtmlEncode(helpText.Header);
                    txtDescription.Text = Server.HtmlEncode(helpText.Description);
                }
                else
                {
                    txtHeader.Text = "";
                    txtDescription.Text = "";
                }
                
            }
        }

        /// <summary>
        /// Update button event handler
        /// Saves updated help text or nullifies it if the text is removed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnUpdate_Click(object sender, EventArgs e)
        {
            // Create new HelpText object with new values, or keep null if it is blank
            HelpText helpText = null;
            if (txtHeader.Text != String.Empty || txtDescription.Text != String.Empty)
            {
                helpText = new HelpText();
                helpText.Header = Server.HtmlDecode(txtHeader.Text);
                helpText.Description = Server.HtmlDecode(txtDescription.Text);
            }

            // Get Question ID and save
            string questionID = lbxQuestions.SelectedValue.Split(' ')[0];

            int parsedQuestionID;
            if (int.TryParse(questionID, out parsedQuestionID))
            {
                this._presenter.saveHelpText(parsedQuestionID, helpText);
            }
            
        }


        
	}
}

